import { NextRequest, NextResponse } from 'next/server';
import { VideoService, CategoryService, initializeDatabase } from '@/app/lib/DataBase/database';
import { testConnection } from '@/app/lib/DataBase/db';

// 测试数据库连接
export async function GET() {
  try {
    const isConnected = await testConnection();
    await initializeDatabase();
    return NextResponse.json({ 
      success: true, 
      connected: isConnected,
      message: isConnected ? '数据库连接成功' : '数据库连接失败'
    });
  } catch (error) {
    console.error('数据库连接测试失败:', error);
    return NextResponse.json({ 
      success: false, 
      connected: false,
      message: '数据库连接测试失败',
      error: error instanceof Error ? error.message : '未知错误'
    }, { status: 500 });
  }
}

// 初始化数据库
export async function POST() {
  try {
    await initializeDatabase();
    return NextResponse.json({ 
      success: true, 
      message: '数据库初始化完成' 
    });
  } catch (error) {
    console.error('数据库初始化失败:', error);
    return NextResponse.json({ 
      success: false, 
      message: '数据库初始化失败',
      error: error instanceof Error ? error.message : '未知错误'
    }, { status: 500 });
  }
}
